package cn.ysnk.generateCode;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
 
public class EntityModel {
 
    private String tableName;
    /**
     * 除了主键所有的字段
     */
    private List<EntityColumn> columns;
    /**
     * 主键字段
     */
    private EntityColumn primaryColumn;
    /**
     * 用于查询的字段
     */
    private EntityColumn searchColumn;
    /**
     * 分页查找方法
     */
    private String saveMethod;
    
    /**
     * entity基本类名
     */
    private String baseEntityName;
    /**
     * entity基本类名首字母大写
     */
    private String upBaseEntityName;
    
    /**
     * entity包名，首字母小写
     */
    private String entityFileName;
    
    /**
     * controller包名，首字母小写
     */
    private String controllerFileName;
    
    /**
     * entity类名，首字母大写
     */
    private String upEntityFileName;
    
    /**
     * controller类名，首字母大写
     */
    private String upControllerFileName;
    /**
     * 是否需要模糊查询
     */
    private boolean hasKeywordsSearch = false;
    
    public EntityModel(String tableName, List<EntityColumn> columns, EntityColumn primaryColumn,
			EntityColumn searchColumn) {
		super();
		this.tableName = tableName;
		this.columns = columns;
		this.primaryColumn = primaryColumn;
		this.searchColumn = searchColumn;
		this.hasKeywordsSearch = (searchColumn!=null);
		if(tableName.indexOf(PropKit.get("tabelPrefix"))>=0){
			baseEntityName = MyStringUtil.underlineToCamelhumpFirstLow(tableName.substring(PropKit.get("tabelPrefix").length()));
			entityFileName = baseEntityName+"Model";
			controllerFileName  = baseEntityName+"Controller";
		}else{
			baseEntityName = MyStringUtil.underlineToCamelhumpFirstLow(tableName);
			entityFileName = baseEntityName+"Model";
			controllerFileName = baseEntityName+"Controller";
		}
		upBaseEntityName = MyStringUtil.captureName(baseEntityName);
		this.upEntityFileName = MyStringUtil.captureName(entityFileName);
		this.upControllerFileName = MyStringUtil.captureName(controllerFileName);
		this.saveMethod = generateSaveMethod();
	}
    
	public String getUpBaseEntityName() {
		return upBaseEntityName;
	}



	public void setUpBaseEntityName(String upBaseEntityName) {
		this.upBaseEntityName = upBaseEntityName;
	}



	public String getBaseEntityName() {
		return baseEntityName;
	}

	public void setBaseEntityName(String baseEntityName) {
		this.baseEntityName = baseEntityName;
	}

	public String getUpEntityFileName() {
		return upEntityFileName;
	}

	public void setUpEntityFileName(String upEntityFileName) {
		this.upEntityFileName = upEntityFileName;
	}

	public String getUpControllerFileName() {
		return upControllerFileName;
	}

	public boolean isHasKeywordsSearch() {
		return hasKeywordsSearch;
	}

	public void setHasKeywordsSearch(boolean hasKeywordsSearch) {
		this.hasKeywordsSearch = hasKeywordsSearch;
	}

	public void setUpControllerFileName(String upControllerFileName) {
		this.upControllerFileName = upControllerFileName;
	}



	public static void main(String[] args) {
//    	List<EntityColumn> colunms = new ArrayList<EntityColumn>();
//    	colunms.add(new EntityColumn(false, ColumnEnum.VARCHAR, "title", ""));
//    	colunms.add(new EntityColumn(true, ColumnEnum.VARCHAR, "name", ""));
//    	EntityModel model = new EntityModel("clz_test", colunms,new EntityColumn(false, ColumnEnum.VARCHAR, "id", ""));
//    	System.out.println(model.saveMethod);
	}
    
 
    public EntityColumn getPrimaryColumn() {
		return primaryColumn;
	}

	public void setPrimaryColumn(EntityColumn primaryColumn) {
		this.primaryColumn = primaryColumn;
	}

	private String generateSaveMethod(){
    	String result = "";
    	Configuration config = new Configuration();
    	Writer writer  = new StringWriter();
    	try {
			config.setDirectoryForTemplateLoading(new File(PathKit.getRootClassPath()));
			Template template = config.getTemplate("save.ftl");
			ConcurrentMap<String, EntityModel> modelMap = new ConcurrentHashMap<String, EntityModel>();
			modelMap.put("model", this);
			template.process(modelMap,writer);
			result = writer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}finally{
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	return result;
    }
 
    public String getTableName() {
        return tableName;
    }
 
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
 
    public String getSaveMethod() {
		return saveMethod;
	}

	public void setSaveMethod(String saveMethod) {
		this.saveMethod = saveMethod;
	}

	public List<EntityColumn> getColumns() {
		return columns;
	}

	public void setColumns(List<EntityColumn> columns) {
		this.columns = columns;
	}

	public EntityColumn getSearchColumn() {
		return searchColumn;
	}

	public void setSearchColumn(EntityColumn searchColumn) {
		this.searchColumn = searchColumn;
	}


	public String getEntityFileName() {
		return entityFileName;
	}


	public void setEntityFileName(String entityFileName) {
		this.entityFileName = entityFileName;
	}


	public String getControllerFileName() {
		return controllerFileName;
	}


	public void setControllerFileName(String controllerFileName) {
		this.controllerFileName = controllerFileName;
	}

 
}